package sk.tuke.homescreen;

import sk.tuke.android4all.controls.CustomFragment;
import sk.tuke.android4all.listeners.SwipeListener;
import sk.tuke.android4all.services.Tts;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;

public class QuickPickAssignmentFragment extends CustomFragment {

	private static String TAG = "QuickPickAssignmentFragment";
	private Context mContext = null;
	private Activity mActivity = null;
	private String mAppFullPackageName = null;
	private String mApplicationLabel = null;
	private Tts mSpeechEngine = null;
	private Vibrator mVibrator = null;
	private GeneralContext mGeneralContext = null;
	
	
	public QuickPickAssignmentFragment(String aAppLabel_in, String aAppFullPackageName_in, GeneralContext aContextObj_in){
		this.mActivity = aContextObj_in.getActivity();
		this.mContext = mActivity.getApplicationContext();
		this.mApplicationLabel = aAppLabel_in;
		this.mAppFullPackageName = aAppFullPackageName_in;
		mSpeechEngine = aContextObj_in.getSpeechEngine();
		mVibrator = aContextObj_in.getVibrateService();
		mGeneralContext = aContextObj_in;
	}
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_quickpick, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);        
        this.getView().setOnTouchListener(mOnTouchListener);
        mSpeechEngine.sayMessage(getResources().getString( R.string.quickpickassign_intro ));
        try {
			Thread.sleep(2500);
		} catch (InterruptedException e) {
			//Do nothing
		}
        mVibrator.vibrate(1000);
        mOnTouchListener.resetTouchCounter();
    }
    
    
    private SwipeListener mOnTouchListener = new SwipeListener(mContext){

		@Override
		public void onDoubleTapAction(int touchCount) {
			Log.d(TAG, "Touches detected: " + touchCount);
			mSpeechEngine.sayMessage(getResources().getString(R.string.quickpickassign_detected) + 
									String.valueOf(touchCount) + 
									getResources().getString(R.string.quickpickassign_detected_end) +
									mApplicationLabel);
            mVibrator.vibrate(500);	
            new QuickPickAssignments(mActivity.getApplicationContext()).addAssignment(touchCount, mAppFullPackageName );
            mActivity.getFragmentManager().beginTransaction().replace(R.id.container, new MenuFragment()).commit();
		}

		@Override
		public void onZoomInAction() {
			Log.i(TAG, "OnZoomInAction");
			mSpeechEngine.sayMessage(mContext.getResources().getString(R.string.voice_quickpick_closed));
			mActivity.getFragmentManager().beginTransaction().replace(R.id.container, new MenuFragment()).commit();		
		}

		@Override
		public void onZoomOutAction() {
			// TODO Auto-generated method stub			
		}
    };


	@Override
	public void returnAction() {
		Log.i(TAG, "returnAction");
		if(getFragmentManager() != null){		
			getFragmentManager().beginTransaction().replace(R.id.container, new MenuFragment()).commit();	
		}
	}
    
}
